package com.four.reimbursemanage.service.impl;

import com.four.reimbursemanage.common.bean.Page;
import com.four.reimbursemanage.dao.WorkapplyMapper;
import com.four.reimbursemanage.entity.OutmoneyApply;
import com.four.reimbursemanage.entity.Workapply;
import com.four.reimbursemanage.service.WorkapplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkapplyServiceImpl implements WorkapplyService {
    @Autowired
    private WorkapplyMapper workapplyMapper;
    @Override
    public Page<Workapply> getOnrePage(int pageNum, int pageSize, String name, Date month, Integer state) {
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Workapply> list=workapplyMapper.selectWorkapplyList(start,pageSize,name,month,state);
        //获取总条目数
        int total=workapplyMapper.countWorkapply(name,month,state);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Workapply> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    @Override
    public Page<Workapply> getOnrePageUser(int pageNum, int pageSize, String name, Date month, Integer state, String number) {
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Workapply> list=workapplyMapper.selectUser(start,pageSize,name,month,state,number);
        //获取总条目数
        int total=workapplyMapper.countWorkapplyUser(name,month,state,number);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Workapply> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    @Override
    public boolean insertWorkapply(Workapply workapply) {
        if (workapply == null){
            return false;
        }
        return workapplyMapper.insertWorkapply(workapply)>0;
    }

    @Override
    public boolean updateWorkapply(Workapply workapply) {
        if (workapply == null) {
            return false;
        }
        return workapplyMapper.updateWorkapply(workapply)>0;
    }

    /*撤销*/
    @Override
    public boolean updateC(Workapply workapply) {
        if (workapply == null) {
            return false;
        }
        workapply.setState(3);
        return workapplyMapper.updateStateById(workapply)>0;
    }
}
